How can I apply IF...THEN in an SQL SELECT?
How can I apply IF...THEN in an SQL SELECT?
550
12-Apr-2023
Updated on 13-Apr-2023
Krishnapriya Rajeev
13-Apr-2023In SQL, you can use the CASE expression to implement conditional logic in a SELECT statement, similar to the IF...THEN construct in other programming languages. Here's an example SQL query that demonstrates how to use the CASE expression in a SELECT statement:
In this query, the CASE expression is used to determine the value of column4 based on the value of column3. If column3 is greater than 0, the value of column4 is set to 'Positive', if column3 is less than 0, the value of column4 is set to 'Negative', and if column3 is equal to 0, the value of column4 is set to 'Zero'. The resulting SELECT statement returns four columns: column1, column2, column3, and column4.